package com.shanghaionstar.dealersorder.presenter;

import android.content.Context;
import android.text.TextUtils;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.dealersorder.bean.OrderDealerRequestEntity;
import com.shanghaionstar.dealersorder.model.OrderModel;
import com.shanghaionstar.dealersorder.view.OrderView;

import rx.functions.Action1;

/**
 * Created by Yonglang Hong on 2016/8/12.
 */
public class OrderPresenter extends BasePresenter {

    OrderView  orderView;

    OrderModel orderModel;

    public void setOrderView(OrderView orderView) {
        this.orderView = orderView;
    }

    public OrderPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        orderModel = new OrderModel(context);
    }

    public void commitOrder(OrderDealerRequestEntity orderEntity) {
        subscription = orderModel.commitOrder(orderEntity)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String orderNum) {
                        if (!TextUtils.isEmpty(orderNum)){
                            orderView.showOrderInfo(orderNum);
                        }else {
                            orderView.showOrderFailed();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        orderView.showErr(throwable);
                    }
                });
    }
}
